package org.easy.alipay;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.app.models.AlipayTradeAppPayResponse;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.alipay.easysdk.payment.wap.models.AlipayTradeWapPayResponse;
import org.easy.core.exception.BizRuntimeException;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;


public class AliPayTemplate {

    public boolean verifyNotify(HttpServletRequest request) {
        Map<String, String> params = convertToMap(request);
        return verifyNotify(params);
    }

    public boolean verifyNotify(Map<String, String> params) {
        try {
            return Factory.Payment.Common().verifyNotify(params);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static Map<String, String> convertToMap(HttpServletRequest request) {
        Map<String, String> params = new HashMap<>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (String name : requestParams.keySet()) {
            params.put(name, request.getParameter(name));
        }
        return params;
    }

    /**
     * 面对面支付预创建订单（扫码支付）
     *
     * @param outTradeNo
     * @param subject
     * @param totalAmount
     * @return
     */
    public String faceToFacePreCreate(String outTradeNo, String subject, BigDecimal totalAmount) {
        try {
            AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace().preCreate(subject, outTradeNo, totalAmount.toString());
            if (ResponseChecker.success(response)) {
                return response.qrCode;
            } else {
                throw new BizRuntimeException(response.getMsg());
            }
        } catch (Exception e) {
            throw new BizRuntimeException(e.getMessage());
        }
    }


    /**
     * app支付
     *
     * @param subject
     * @param outTradeNo
     * @param totalAmount
     * @return 支付链
     */
    public String appPay(String subject, String outTradeNo, BigDecimal totalAmount) {
        try {
            AlipayTradeAppPayResponse response = Factory.Payment.App().pay(subject, outTradeNo, totalAmount.toString());
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                return response.getBody();
            } else {
                throw new BizRuntimeException("err appPay " + outTradeNo);
            }
        } catch (Exception e) {
            throw new BizRuntimeException(e.getMessage());
        }
    }


    /**
     * 手机网页支付
     *
     * @param subject
     * @param outTradeNo
     * @param totalAmount
     * @param quitUrl
     * @param returnUrl
     * @return
     */
    public String wapPay(String subject, String outTradeNo, BigDecimal totalAmount, String quitUrl, String returnUrl) {
        try {
            AlipayTradeWapPayResponse response = Factory.Payment.Wap().pay(subject, outTradeNo, totalAmount.toString(), quitUrl, returnUrl);
            if (ResponseChecker.success(response)) {
                return response.getBody();
            } else {
                throw new BizRuntimeException("err appPay " + outTradeNo);
            }
        } catch (Exception e) {
            throw new BizRuntimeException(e.getMessage());
        }
    }


    /**
     * 电脑网页支付
     *
     * @param subject
     * @param outTradeNo
     * @param totalAmount
     * @param returnUrl
     * @return
     */
    public String pagePay(String subject, String outTradeNo, BigDecimal totalAmount, String returnUrl) {
        try {
            AlipayTradePagePayResponse response = Factory.Payment.Page().pay(subject, outTradeNo, totalAmount.toString(), returnUrl);
            if (ResponseChecker.success(response)) {
                return response.getBody();
            } else {
                throw new BizRuntimeException("err appPay " + outTradeNo);
            }
        } catch (Exception e) {
            throw new BizRuntimeException(e.getMessage());
        }
    }

}
